﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Pr0ject0.Models
{
    public class StoreDB
    {
        private string connection;
        private List<StoreModel> stores;
        public List<StoreModel> StoreList { get{return new List<StoreModel>(stores);} }
   
        public StoreDB()
        {
            connection = "DB";
            update();
        }

        public StoreDB(string connectionString)
        {
            connection = connectionString;
            update();
        }

        public void update()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[connection].ToString());
            SqlCommand cmd = new SqlCommand("SELECT * FROM [Store]", conn);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            stores = new List<StoreModel>();

            while (reader.Read())
            {
                StoreModel model = new StoreModel();
                model.Id = Convert.ToInt32(reader["StoreID"].ToString());
                model.name = reader["Store"].ToString();
                model.address = reader["Address"].ToString();
                model.city = reader["City"].ToString();
                model.state = reader["State"].ToString();
                model.zip = reader["Zip"].ToString();
                model.longitude = Convert.ToDouble(reader["Longitude"].ToString());
                model.latitude = Convert.ToDouble(reader["Latitude"].ToString());
                model.minimumWage = Convert.ToDouble(reader["MinimumWage"].ToString());
                stores.Add(model);
            }

            reader.Close();
            conn.Close();
        }
    }
}